//import { createApp } from '../vue/vue.global.js'

const pageinit = "page-item"
const pagedisabled = "page-item disabled"


function addr_Joining(...array) {
	let url = "/"
	for (let i=0;i<array.length;i++) {
		url += array[i]
	}
	return url
}


function mrecursive(marray) {
  switch(marray[marray[marray.length-1]]){
	  case true:
	    return marray[marray.length-1]+1
	  case false:
	    if (marray.length > 3) {
	      marray[marray.length-1]++
	      return mrecursive(marray)
	    }else{
	      return marray[marray.length-1]+1
		}
	}
}

const app = {
  data(){
		return {
    	mlimit : 30, 						//页数
    	mpage : 0,							//当前页
      todos : [],							//表格对象
      pagelist : [],					//分页属性数组
      page_on : pageinit, 		//上一页属性
      page_down : pageinit,		//下一页属性	
      msearch : ""
		}
	},
  created() {						//初始化当前页
  	let session_page = sessionStorage.getItem("mpage")
  	if (session_page != null) {
  		this.mpage = parseInt(session_page)
  	}
    this.addNewTodo(this.mpage)
    if (localStorage.getItem("user") == null) {
      axios.get(addr_Joining("api/", "user"), {
	      params: {
	      	model: "get-user"
	      }
	    }).then(
	      res=>{
	      	localStorage.setItem("user", res.data['msg'])
	      }
	    ).catch(
	      error=>{
	      	console.log(error)
	      }
	    )
    }  
  },
	methods: {
		addNewTodo(mpages) {
			let newtime = new Date().getTime()
      axios.get(addr_Joining("api/", "check?", "newtime=", newtime), {
      	params: {
      		limit:  this.mlimit,
      	  page :  mpages,
      	 	search: this.msearch
      	}
      }).then(
        res=>{
          let count = res.data["page"]
          this.todos = res.data["msg"]
          switch (mrecursive([this.mpage+1 >= count, this.mpage < 1, true, 0])){
          	case 1:
          		this.page_down = pagedisabled
          		this.page_on = pageinit
          		if (count == 1) {
          			this.page_on = pagedisabled
          		}
          		break
          	case 2:
          		this.page_on = pagedisabled
          		this.page_down = pageinit
          		break
          	case 3:
							this.page_on = pageinit
							this.page_down = pageinit
							break
          }
          switch (mrecursive([this.mpage <= 3, this.mpage + 3 > count, true, 0])) {
          	case 1:
							if (count>6) {
          			this.pagelist = this.on_generate(7)
          		}else{
          			this.pagelist = this.on_generate(count)
          		}
          		break
          	case 2:
          		this.pagelist = this.on_generate(count)
          		break
          	case 3:
          		this.pagelist = this.on_generate(this.mpage + 3)
          		break
          }
          //console.log(this.todos.length)
        }
    	).catch(
        error=>{
          console.log(error)
      })
    },
		on_addpage(){				//跳转下一页
			this.mpage += 1
			this.addNewTodo(this.mpage)
		},
		on_delpage(){				//跳转上一页
			this.mpage -= 1
			this.addNewTodo(this.mpage)
		},
		on_clickPage(val) {			//跳转选择页
			this.mpage = val-1
			this.addNewTodo(this.mpage)
		},
		on_setpage(val){		//修改分页页数
			this.mlimit = val
			this.addNewTodo(this.mpage)
		},
		on_download(val) {
			sessionStorage.setItem("mpage",this.mpage)
      		return "/download?id=" + val
    },
    on_search() {	//搜索
    	this.msearch = this.$refs.vue_search.value
    	this.addNewTodo(0)
    },
		on_generate(val){  	//修改分页属性
			let _pagelist = []
			for (let i=6; i>-1; i--) {
				let _class = pageinit, linkclass = "page-link"
				if (val-i == this.mpage+1) {
					_class = pagedisabled
					linkclass = linkclass + " bg-info"
				}
				if (val-i>0){
					_pagelist.push({id: val-i, class: _class, linkclass: linkclass})
				}
			}
			return _pagelist
		}
	}
}

var Ctor = Vue.extend(app)
new Ctor().$mount('#app')